package controller.log;

import base.response.OffsetLimitPage;
import controller.common.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import services.log.LoginLogServices;
import util.DateUtil;
import wt.entity.log.LoginLogExample;

import java.util.Date;

/**
 * 功能:
 * 作者: 洪世成
 * 日期: 2017/9/9
 * 版权所有: admin
 */
@Controller
@RequestMapping("log/loginLog")
public class LoginLogController extends BaseController {
    @Autowired
    LoginLogServices loginLogServices;


    @RequiresPermissions("log:loginLog:default")
    @RequestMapping("index")

    public String index() {
        return "log/loginLog/index";
    }




    @RequiresPermissions("log:loginLog:default")
    @ResponseBody
    @RequestMapping("select")
    public OffsetLimitPage select(
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime,
            String loginName,
            String ip,
            Short loginResult,
            Integer offset,
            Integer limit
    ) throws Exception {
        LoginLogExample loginLogExample = new LoginLogExample();
        loginLogExample.setOrderByClause("id desc" );
        LoginLogExample.Criteria criteria = loginLogExample.createCriteria();
        if (startTime != null) {
            criteria.andLoginTimeGreaterThanOrEqualTo(startTime);
        }
        if (endTime != null) {
            criteria.andLoginTimeLessThan(DateUtil.moveDate(endTime,1));
        }
        if (!StringUtils.isBlank(loginName)) {
            criteria.andLoginNameEqualTo(loginName);
        }
        if (!StringUtils.isBlank(ip)) {
            criteria.andIpEqualTo(ip);
        }
        if (loginResult!=null) {
            criteria.andLoginResultEqualTo(loginResult);
        }
        return loginLogServices.selectByExample(loginLogExample,offset,limit);

    }

}
